#!/bin/bash

vvsfs_build=false
vvsfs_remove=false
vvsfs_install=false
vvsfs_clean=false
vvsfs_mount=false
vvsfs_umount=false
vvsfs_default=true
vvsfs_mount_result="";
vvsfs_install_result="";

if [ $# -gt '1' ]; then
        echo "$0: Cannot launch with more than one arguments."
        exit 1;
fi

if [ $# -gt '0' ]; then
        vvsfs_default=false;
	for arg in "$@"
	do
		if [ $arg == '-b' -o $arg == '--build' ]; then
			vvsfs_build=true;
		elif [ $arg == "-c" -o $arg == '--clean' ]; then
			rm -f *.vvsfs
			rm *.ko
			rm vvsfs.o
			rm vvsfs.mod*
			rm *~
			rm vvsfs.raw
			rm modules.order
			rm Module.symvers
			rm built-in.o
			rm ioctl_decrypt
			rm ioctl_encrypt
			rm ioctl_size
			rm mmap_test
			rm truncate
			rm hello
			rm hello.o
			rm .vvsfs.*
			rm -f *.img
			rm .built-in.o.cmd
			exit 0;
		elif [ $arg == "-p" -o $arg == '--print' ]; then
			./view.vvsfs vvsfs.raw | less -S
			exit 0;
		elif [ $arg == "-i" -o $arg == '--install' ]; then
			vvsfs_install=true;
		elif [ $arg == "-m" -o $arg == '--mount' ]; then
			vvsfs_mount=true;
		elif [ $arg == "-u" -o $arg == '--umount' -o $arg == '--unmount' ]; then
			vvsfs_umount=true;
		elif [ $arg == "-r" -o $arg == '--remove' ]; then
			vvsfs_remove=true;
		elif [ $arg == "-g" -o $arg == '--generate' ]; then
		        if [ ! -f "`pwd`/mkfs.vvsfs" ]; then
		                echo "Cannot find `pwd`/mkfs.vvsfs"
		                exit 1;
		        fi
		        echo "Building vvsfs.raw..."
		        dd of=`pwd`/vvsfs.raw if=/dev/zero bs=1024 count=200
		        ./mkfs.vvsfs vvsfs.raw
		        exit 0;
		elif [ $arg == "-h" -o $arg == "--help" ]; then
			echo "Usage:$0 [OPTION]..."
			echo "Auto management script of vvsfs."
			echo "If no arguments is input, it will launch -u, -r, -b, -m in order."
			echo "  -b, --build                build the vvsfs"
			echo "  -c, --clean                clean the build of vvsfs"
			echo "  -g, --generate             generate the raw file"
			echo "  -h, --help                 print out this message"
			echo "  -i, --install              install the vvsfs module"
			echo "  -m, --mount                install vvsfs and mount the testdir"
			echo "  -p, --print                show the vvsfs.raw file content"
			echo "  -r, --remove               remove the vvsfs module"
			echo "  -u, --umount, --unmount    unmount the testdir"
			exit 0;
		fi
	done
fi

if [ $vvsfs_default == true -o $vvsfs_umount == true ]; then
        vvsfs_mount_result=`cat /proc/mounts | grep vvsfs`;
        if [[ -z $vvsfs_mount_result ]]; then
                echo "No testdir is mounted."
        else
               	echo "Removing mount..."
              	sudo umount -f ./testdir
                echo "Removing vvsfs module..."
               	sudo rmmod -f vvsfs
               	if [ -d "`pwd`/testdir" ]; then
               	        echo "Removing the testdir..."
               	        rm -rf testdir
               	fi
        fi
fi

if [ $vvsfs_default == true -o $vvsfs_build == true ]; then
        echo "Building vvsfs..."
        make
fi

if [ $vvsfs_default == true -o $vvsfs_install == true -o $vvsfs_mount == true ]; then
        vvsfs_install_result=`cat /proc/filesystems | grep vvsfs`;
        if [[ -z $vvsfs_install_result ]]; then
                echo "Installing vvsfs module..."
		sudo modprobe lz4_compress
                sudo insmod vvsfs.ko
        else
                echo "vvsfs is already installed."
        fi
fi

if [ $vvsfs_default == true -o $vvsfs_mount == true ]; then
        vvsfs_mount_result=`cat /proc/mounts | grep vvsfs`;
        if [[ -z $vvsfs_mount_result ]]; then
                if [ ! -d "`pwd`/testdir" ]; then
                        echo "Creating testdir..."
                        mkdir testdir
                fi
		echo "Cleaning system log"
		sudo dmesg --clear
                echo "Mounting file system..."
                sudo mount -t vvsfs -o loop vvsfs.raw testdir/
        else
                echo "testdir is already mounted."
        fi
fi
